// pages/aRed/aRed.js
var service_common = require("../../service/common.js");
var service_user = require("../../service/user.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    redID: -1,
    record: { already_get: false },
    HBao: {},
    userInfo: {},
    DataList: [],
    page: 1,
    offon: true,
    command: "",
    tc_userInfo: false,
    options:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    wx.setStorageSync("isred", true)
    if (options.data != undefined && options.data != 0){
      var optionss = JSON.parse(options.data)
      console.log(options)
      getApp().globalData.uniq_openid = optionss.uniq_openid
      wx.setStorageSync('token__', optionss.token)
      wx.setStorageSync('token_key', optionss.token_key)
      service_common.fetchData(
        service_common.getUrl("user.getUserMpInfo"), {
          openid: optionss.openid,
          options: options
        },
        (res) => {
          if (res.data.code == 0) {
            wx.setStorageSync('userInfo', res.data.data);
            getApp().globalData.userInfo = res.data.data
            getApp().globalData.openid = res.data.data.openid
            getApp().globalData.unionid = res.data.data.unionid
            this.setData({
              redID: options.id
            })
            this.getRedMsg()
            this.getRedPacketDetailList()
          }
        },
        "GET"
      )
    }else{
      this.setData({
        redID: options.id
      })
      this.getRedMsg()
      this.getRedPacketDetailList()
    }
    
  },

  goHome:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  login: function () {
    service_user.login((res) => {
      if (res == false) {//未授权
        wx.hideLoading()
        this.setData({
          tc_userInfo: true
        })
      }else{
        this.getRedMsg()
        this.getRedPacketDetailList()
      }
    }, this.data.openid);
  },
  // 授权
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      this.setData({
        tc_userInfo: false
      })
      this.login()
    }
  },
  /**
   * 获取输入口令
   */
  getcommand: function (e) {
    this.setData({
      command: e.detail.value
    })
  },
  /**
   * 前往下载app
   */
  goapp: function () {
    wx.navigateTo({
      url: '/pages/aApp/aApp',
    })
  },
  /**
   * 前往其他名片
   */
  goOther: function () {
    wx.navigateTo({
      url: '/pages/other/other?openid=' + this.data.userInfo.openid,
    })
  },
  /**
   * 领取红包
   */
  redPacketGet: function () {
    service_common.fetchData(
      service_common.getUrl("hb.redPacketGet"),
      {
        openid: getApp().globalData.openid,
        fromid: this.data.redID,
        command: this.data.command
      },
      (res) => {
        if (res.data.code == 0) {
          this.setData({
            offon: true,
            page: 1
          })
          this.getRedMsg()
          this.getRedPacketDetailList()
        }
      },
      "GET"
    )
  },
  /**
   * 获取红包详情
   */
  getRedMsg: function () {
    service_common.fetchData(
      service_common.getUrl("hb.redPacketDetailGet"),
      {
        openid: getApp().globalData.openid,
        id: this.data.redID
      },
      (res) => {
        if (res.data.code == 0) {
          this.setData({
            HBao: res.data.data.hongBao,
            record: res.data.data.record,
            userInfo: res.data.data.user
          })
        }
        console.log(res)
      },
      "GET"
    )
  },
  /**
   * 获取红包详情列表
   */
  getRedPacketDetailList: function () {
    service_common.fetchData(
      service_common.getUrl("hb.getRedPacketDetailList"),
      {
        openid: getApp().globalData.openid,
        id: this.data.redID,
        page: this.data.page
      },
      (res) => {
        if (res.data.code == 0) {
          if (this.data.page == 1) {
            this.setData({
              DataList: res.data.data.list
            })
          } else {
            if (res.data.data.list.length != 0) {
              this.data.DataList = this.data.DataList.concat(res.data.data.list)
              this.setData({
                offon: true,
                DataList: this.data.DataList
              })
            }
          }
        }
        console.log(res)
      },
      "GET"
    )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    if (this.data.options.data != undefined && this.data.options.data != '0') {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.offon) {
      this.data.page += 1
      this.setData({
        page: this.data.page,
        offon: false
      })
      this.getRedPacketDetailList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // 设置转发的路径
    var share_path = 'pages/auth/auth?webUrl=aRed&zf_openid=' + getApp().globalData.userInfo.openid + "&redId=" + this.data.redID + "&r=" + Math.random(100000);
    return {
      title: "您的好友发来一个口令红包，口令：" + this.data.HBao.command,
      desc: '您的好友发来一个口令红包',
      path: share_path,
      success: () => {
        this.redPacketGet()
      },
      fail: res => {
        wx.showToast({
          title: '领取失败',
          image: '/image/cancel.png',
          duration: 2000
        });
      }
    }
    
  }
})